package com.home.auto_navigation.ui.controllers;

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;

import com.home.auto_navigation.common.Driver;

public abstract class AbstractPageElement implements Element {

	private Driver driver = Driver.getInstance();
	protected final WebElement wrappedElement;
	protected final String name;

	public AbstractPageElement(final WebElement wrappedElement,
			final String name) {
		this.wrappedElement = wrappedElement;
		this.name = name;
	}

	public String getTagName() {
		if (wrappedElement != null) {
			return wrappedElement.getTagName();
		} else
			return null;
	}

	public String getAttribute(String name) {
		if (wrappedElement != null) {
			highlightElement();
			return wrappedElement.getAttribute(name);
		} else
			return null;
	}

	public boolean isPresent() {
		if (wrappedElement != null) {
			try {
				return wrappedElement.isDisplayed();
			} catch (NoSuchElementException e) {
				return false;
			}
		} else
			return false;
	}

	public WebElement getWebElement() {
		return wrappedElement;
	}

	public void highlightElement() {
		String bg = wrappedElement.getCssValue("backgroundColor");
		for (int i = 0; i < 3; i++) {
			driver.executeScript("arguments[0].style.backgroundColor = '"
					+ "red" + "'", wrappedElement);
			driver.executeScript("arguments[0].style.backgroundColor = '" + bg
					+ "'", wrappedElement);

		}
	}
}
